package com.maishi.programmer.jainzhioffer;

/**
 * Created by brossdong on 2018/5/5.
 * 替换空格
 */
public class TestProblem5 {
    /**
     * 请实现一个函数，将一个字符串中的空格替换成“%20”。
     * 例如，当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
     * @param str
     * @return
     */
    public static String replaceSpace(StringBuffer str) {

        if (str == null) return null;
        for (int i = 0;i < str.length();i++) {
            if (str.charAt(i) == ' ') {
                str.replace(i,i+1,"%20");
            }
        }
        return str.toString();
    }

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("We Are Happy.");
        System.out.println(replaceSpace(sb).toString());

    }
}
